package com.wentao.mall.controller;

import com.github.pagehelper.PageInfo;
import com.wentao.mall.commons.Constant;
import com.wentao.mall.commons.Result;
import com.wentao.mall.exception.MallException;
import com.wentao.mall.exception.MallExceptionEnum;
import com.wentao.mall.model.dto.AddProductRequest;
import com.wentao.mall.model.dto.ProductListRequest;
import com.wentao.mall.model.dto.UpdateProductRequest;
import com.wentao.mall.model.po.Product;
import com.wentao.mall.service.ProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation("后台添加商品")
    @PostMapping("/admin/product/add")
    public Result addProduct(@RequestBody @Valid AddProductRequest addProductRequest){
        Product product=new Product();
        BeanUtils.copyProperties(addProductRequest,product);
        productService.addProduct(product);
        return Result.success("添加商品成功");
    }

    @ApiOperation("图片上传")
    @PostMapping("admin/upload/file")
    public Result uploadProductImage(@RequestParam("file")MultipartFile file, HttpServletRequest req) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String ext =originalFilename.substring(originalFilename.lastIndexOf("."));
        //获得新文件的名字
        String newName= UUID.randomUUID().toString()+ext;
        File dir = new File(Constant.FILE_PATH);
        if (!dir.exists()){
            throw new MallException(MallExceptionEnum.DIR_ERROR);
        }
        File destFile = new File(dir,newName);
        file.transferTo(destFile);
        //  http://127.0.0.1/
        String serverFilePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/images/";
        String fileUrl = serverFilePath+newName;
        return Result.success(fileUrl);
    }

    @ApiOperation("修改商品")
    @PostMapping("/admin/product/update")
    public Result updateProduct(@RequestBody @Valid UpdateProductRequest updateProductRequest){
        Product product = new Product();
        BeanUtils.copyProperties(updateProductRequest,product);
        productService.updateProduct(product);
        return Result.success("修改成功");
    }

    @ApiOperation("删除商品")
    @PostMapping("/admin/product/delete")
    public Result deleteProduct(@RequestParam Integer id){
        productService.deleteProduct(id);
        return Result.success("删除成功");
    }

    @ApiOperation("商品批量上下架")
    @PostMapping("admin/product/batchUpdateSellStatus")
    public Result batchUpdateSellStatus(@RequestParam("ids") Integer[] ids,Integer sellStatus){
        productService.batchUpdateSellStatus(ids,sellStatus);
        return Result.success("商品批量上下架成功");
    }

    @ApiOperation("后台分页列表")
    @GetMapping("admin/product/list")
    public Result listForAdmin(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize){
        PageInfo pageInfo = productService.listForAdmin(pageNum,pageSize);
        return Result.success(pageInfo);
    }

    @ApiOperation("前台商品详情")
    @GetMapping("product/detail")
    public Result detail(@RequestParam("id") Integer id){
        Product product = (Product)productService.detail(id);
        return Result.success(product);
    }

    @ApiOperation("前台商品列表")
    @GetMapping("product/list")
    public Result listForCustomer(ProductListRequest productListRequest){
        PageInfo pageInfo=productService.listForCustomer(productListRequest);
        return Result.success(pageInfo);
    }
}
